//
//  MessageCircleFriendsTableViewCell.m
//  YunDong55like
//
//  Created by junseek on 15/8/3.
//  Copyright (c) 2015年 五五来客 lj. All rights reserved.
//

#import "MessageCircleFriendsTableViewCell.h"
#import "XHImageUrlViewer.h"

@interface MessageCircleFriendsTableViewCell ()<UIAlertViewDelegate>{
    //
    NSIndexPath *tempIndexPath;
    NSDictionary *tempDic;

    
    XHImageUrlViewer *imageViewer;
    
    
    UIButton *btnLeftZan;
    UIButton *btnRightEvaluation;
    UIButton *btnPraise;
    
    UIImageView *userLogo;
    UILabel *lblName;
    UILabel *lblContent;
    UILabel *lblDate;
    UIButton *btnDelete;
    
    NSInteger ft_cellH;
    
}


@end

@implementation MessageCircleFriendsTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{
    if (self= [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
         self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setBackgroundColor:RGBCOLOR(255, 255, 255)];
        
        [self addSubview:[RHMethods lineViewWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)]];
        
        userLogo=[RHMethods imageviewWithFrame:CGRectMake(10, 10, 40,40) defaultimage:@""];
        userLogo.userInteractionEnabled=YES;
        userLogo.layer.masksToBounds = YES;
        userLogo.layer.cornerRadius =(W(userLogo))/2.0;
        userLogo.layer.borderWidth=0.1;
        userLogo.layer.borderColor =[[UIColor clearColor] CGColor];
        [self addSubview:userLogo];
        
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickedUserLogo:)];
        [userLogo addGestureRecognizer:tap];
        
        
        lblName=[RHMethods labelWithFrame:CGRectMake(XW(userLogo)+10, Y(userLogo), kScreenWidth-XW(userLogo)-20-40, 20) font:fontTitle color:rgbBlue text:@"小小小小想"];
        [self addSubview:lblName];
        
        lblDate=[RHMethods labelWithFrame:CGRectMake(X(lblName), YH(lblName), 200, 20) font:fontSmallTitle color:rgbTxtGray text:@"3分钟前"];
        [self addSubview:lblDate];
        
        lblContent=[RHMethods labelWithFrame:CGRectMake(X(lblName), YH(userLogo)+10,kScreenWidth-20-XW(userLogo), 0) font:fontTxtContent color:rgbTitleColor text:@"  "];//[NSString stringWithFormat:@"%@",
        [self addSubview:lblContent];
        
        NSInteger fty=YH(lblContent)+10;
        float ftw=70*(kScreenWidth/320.0);//(ftx-f_JG*3)/3;//每行固定显示3个
        for (int i=0; i<9; i++) {
            UIImageView *imageV=[RHMethods imageviewWithFrame:CGRectMake(0, 0, ftw, ftw) defaultimage:@""];
            imageV.userInteractionEnabled=YES;
            imageV.tag=10+i;
            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageClicked:)];
            [imageV addGestureRecognizer:tap];
            [self addSubview:imageV];
        }
        
        btnDelete =[RHMethods buttonWithFrame:CGRectMake(kScreenWidth-50, Y(lblDate)-5, 50, 30) title:@"删除" image:@"" bgimage:@""];
        btnDelete.titleLabel.font=fontTxtContent;
        [btnDelete setTitleColor:rgbBlue forState:UIControlStateNormal];
        [btnDelete addTarget:self action:@selector(deleteMessage) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnDelete];
        
        btnRightEvaluation=[RHMethods buttonWithFrame:CGRectMake(kScreenWidth-40, fty, 40, 30) title:@"" image:@"plbtn" bgimage:@""];
        [btnRightEvaluation setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        [btnRightEvaluation setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [self addSubview:btnRightEvaluation];
        [btnRightEvaluation addTarget:self action:@selector(cellRightEvaluationButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        btnPraise=[RHMethods buttonWithFrame:CGRectMake(X(btnRightEvaluation)-35, fty, 35, 30) title:@"" image:@"zan0408" bgimage:@""];
        btnPraise.titleLabel.font=fontTxtContent;
        [btnPraise setTitleColor:rgbBlue forState:UIControlStateNormal];
//        [btnPraise setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
//        [btnPraise setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        [btnPraise setImage:[UIImage imageNamed:@"zan0408Red"] forState:UIControlStateSelected];
        [self addSubview:btnPraise];
        [btnPraise addTarget:self action:@selector(cellPraiseButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
-(void)refreshButtonPraiseStatus:(NSDictionary *)dic{
    
    if ([[dic valueForJSONStrKey:@"praisenum"] notEmptyOrNull] && [[dic valueForJSONStrKey:@"praisenum"] integerValue]>0) {
        [btnPraise setTitle:[NSString stringWithFormat:@" %@",[dic valueForJSONStrKey:@"praisenum"]] forState:UIControlStateNormal];
        float fw=[btnPraise.titleLabel sizeThatFits:CGSizeMake(MAXFLOAT, 20)].width+30;
        btnPraise.frame=CGRectMake(X(btnRightEvaluation)-fw, Y(btnRightEvaluation), fw, 30);
    }else{
        [btnPraise setTitle:@"" forState:UIControlStateNormal];
        btnPraise.frame=CGRectMake(X(btnRightEvaluation)-35, Y(btnRightEvaluation), 35, 30);
    }
    if ([[dic valueForJSONStrKey:@"ispraise"] isEqualToString:@"1"]) {
        btnPraise.selected=YES;
        [btnPraise showAnimationSelected];
    }else{
        btnPraise.selected=NO;
        [btnPraise showAnimationCancelSelected];
    }
    
}
//内容更新
-(NSInteger)setValueForDictionary:(NSDictionary *)dic indexPath:(NSIndexPath *)indexPath{
    return [self setValueForDictionary:dic indexPath:indexPath type:@"show"];
}
//内容更新
-(NSInteger)setValueForDictionary:(NSDictionary *)dic indexPath:(NSIndexPath *)indexPath type:(NSString *)strType{
    tempIndexPath=indexPath;
    tempDic=dic;
    lblName.text=[dic valueForJSONKey:@"realname"];
    if (![strType isEqualToString:@"noImage"]) {
        [userLogo imageSizeWithURL:[dic valueForJSONStrKey:@"avatar"] useProgress:NO useActivity:NO];
    }
    
    lblDate.text =[dic valueForJSONKey:@"ctime"];//[[Utility Share] timeToTimestamp:[dic valueForJSONKey:@"ctime"]];
//    lblDate.frameWidth=[lblDate sizeThatFits:CGSizeMake(MAXFLOAT, 20)].width;
//    btnDelete.frameX=XW(lblDate);

    btnDelete.hidden=![[dic valueForJSONStrKey:@"uid"] isEqualToString:[Utility Utuid]];
    if ([strType isEqualToString:@"show"]) {
        btnPraise.hidden=NO;
        btnPraise.selected=[[dic valueForJSONStrKey:@"ispraise"] isEqualToString:@"1"];
        if ([[dic valueForJSONStrKey:@"praisenum"] notEmptyOrNull] && [[dic valueForJSONStrKey:@"praisenum"] integerValue]>0) {
            [btnPraise setTitle:[NSString stringWithFormat:@" %@",[dic valueForJSONStrKey:@"praisenum"]] forState:UIControlStateNormal];
            float fw=[btnPraise.titleLabel sizeThatFits:CGSizeMake(MAXFLOAT, 20)].width+30;
            btnPraise.frame=CGRectMake(X(btnRightEvaluation)-fw, Y(btnRightEvaluation), fw, 30);
        }else{
            [btnPraise setTitle:@"" forState:UIControlStateNormal];
            btnPraise.frame=CGRectMake(X(btnRightEvaluation)-35, Y(btnRightEvaluation), 35, 30);
        }
        
    }else{
        btnPraise.hidden=YES;
    }
    lblContent.text=[dic valueForJSONKey:@"content"];
    if ([dic valueForJSONKey:@"content"]) {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:lblContent.text];
        NSMutableParagraphStyle *paragraphStyleT = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyleT setLineSpacing:5];//调整行间距
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyleT range:NSMakeRange(0, [lblContent.text length])];
        lblContent.attributedText = attributedString;
    }    
    lblContent.frameHeight=[lblContent sizeThatFits:CGSizeMake( kScreenWidth-70, MAXFLOAT)].height;
    
    NSInteger tempH=YH(lblContent)+5;
    float f_JG=5;//间隔
    float ftx=kScreenWidth-20;
    float ftw=75*(kScreenWidth/320.0);
    NSArray *albums=[dic objectForJSONKey:@"album"];
    for (int i=0; i<9; i++) {
        UIImageView *imageV=(UIImageView *)[self viewWithTag:10+i];
        if (i+1<=[albums count]) {
            imageV.hidden=NO;
            
            if (i%3==0) {
                ftx=X(lblName);
                if (i!=0) {
                    tempH+=ftw+f_JG;
                }
            }
            
            if ([albums count]==1) {
                //当为一张图的时候放大到两张图的位置
                imageV.frame=CGRectMake(X(lblName), tempH, ftw*2+f_JG, ftw*2+f_JG);
                tempH+=ftw*2+f_JG+10;
            }else {
                imageV.frame=CGRectMake(ftx, tempH, ftw, ftw);
                ftx+=ftw+f_JG;
                if (i+1==[albums count]) {
                    tempH+=ftw;//+10
                }
            }
            if (![strType isEqualToString:@"noImage"]) {
                if (i==0) {
                    if ([albums count]==1) {
                        [imageV imageSizeWithURL:[[albums objectAtIndex:i] valueForJSONStrKey:@"path"] useProgress:NO useActivity:NO];
                    }else{
                        [imageV imageSizeWithURL:[[albums objectAtIndex:i] valueForJSONStrKey:@"middle_path"] useProgress:NO useActivity:NO];
                    }
                }else {
                    [imageV imageSizeWithURL:[[albums objectAtIndex:i] valueForJSONStrKey:@"middle_path"] useProgress:NO useActivity:NO];
                }
            }
            
            
        }else{
            imageV.hidden=YES;
        }
    }
    btnRightEvaluation.frameY=tempH;
    btnPraise.frameY=tempH;
    
    return YH(btnRightEvaluation);
    
}

#pragma mark UITapGestureRecognizer
-(void)tapImageClicked:(UITapGestureRecognizer *)tap{
    if ([self.delegate respondsToSelector:@selector(selectImages:)]) {
        [self.delegate selectImages:self];
    }
    UIImageView *img=(UIImageView *)tap.view;
    //放大图片
    if (!imageViewer) {
        imageViewer = [[XHImageUrlViewer alloc] init];
    }
    NSMutableArray *arrayT=[[NSMutableArray alloc]init];
    
    for (int i=0; i<[[tempDic objectForJSONKey:@"album"] count]; i++) {//[[dict objectForJSONKey:@"path"] count]
        UIImageView *imageTemp=(UIImageView *)[self viewWithTag:10+i];
         [arrayT addObject:@{@"DefaultImage":imageTemp.image,@"url":[[[tempDic objectForJSONKey:@"album"] objectAtIndex:i] valueForJSONStrKey:@"path"]}];
        
    }
    // @[@{@"DefaultImage":image,@"DefaultName":@"imageName",@"url":@"http:xxx.jpg"}]
    [imageViewer showWithImageDatas:arrayT selectedIndex:img.tag-10];
//    [imageViewer showWithImageViews:arrayT selectedView:img];
    
}
-(void)clickedUserLogo:(UITapGestureRecognizer *)tap{
    if ([self.delegate respondsToSelector:@selector(selectUserIcon:object:index:)]) {
        [self.delegate selectUserIcon:self object:tempDic index:tempIndexPath];
    }
}
#pragma mark cell button
-(void)cellPraiseButtonClicked:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(selectPraise:object:index:)]) {
        [self.delegate selectPraise:self object:tempDic index:tempIndexPath];
    }
}
-(void)cellRightEvaluationButtonClicked:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(selectEvaluation:object:index:)]) {
        [self.delegate selectEvaluation:self object:tempDic index:tempIndexPath];
    }
}
-(void)deleteMessage{
    //删除   
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"确定要删除这条信息么？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil];
    [alert show];
    
}
#pragma mark alert
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==1) {
        if ([self.delegate respondsToSelector:@selector(deleteMessage:object:index:)]) {
            [self.delegate deleteMessage:self object:tempDic index:tempIndexPath];
        }
    }
}

@end
